/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.makrobioklub.action.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.fyx.core.entity.FyxAddress;

/**
 *
 * @author xljan
 */
@Entity
@Table(name = "action_")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Action.findAll", query = "SELECT a FROM Action a"),
    @NamedQuery(name = "Action.findByIdAction", query = "SELECT a FROM Action a WHERE a.idAction = :idAction"),
    @NamedQuery(name = "Action.findByFromDate", query = "SELECT a FROM Action a WHERE a.fromDate = :fromDate"),
    @NamedQuery(name = "Action.findByToDate", query = "SELECT a FROM Action a WHERE a.toDate = :toDate"),
    @NamedQuery(name = "Action.findByPrice", query = "SELECT a FROM Action a WHERE a.price = :price"),
    @NamedQuery(name = "Action.findByPriceMember", query = "SELECT a FROM Action a WHERE a.priceMember = :priceMember")})
public class Action implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id_action")
    private Integer idAction = 0;
    @Lob
    @Size(max = 65535)
    @Column(name = "name")
    private String name;
    @Lob
    @Size(max = 65535)
    @Column(name = "shortDescription")
    private String shortDescription;
    @Lob
    @Size(max = 65535)
    @Column(name = "description")
    private String description;
    @Column(name = "fromDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fromDate;
    @Column(name = "toDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date toDate;
    @Column(name = "price")
    private Integer price;
    @Column(name = "price_member")
    private Integer priceMember;
    @JoinTable(name = "action_has_price", joinColumns = {
        @JoinColumn(name = "id_action", referencedColumnName = "id_action")}, inverseJoinColumns = {
        @JoinColumn(name = "id_price", referencedColumnName = "id_price")})
    @ManyToMany(cascade= CascadeType.ALL)
    private Collection<ActionPrice> actionPriceCollection;
    @JoinColumn(name = "id_address", referencedColumnName = "id_address")
    @ManyToOne
    private FyxAddress idAddress;

    public Action() {
    }

    public Action(Integer idAction) {
        this.idAction = idAction;
    }

    public Integer getIdAction() {
        return idAction;
    }

    public void setIdAction(Integer idAction) {
        this.idAction = idAction;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getFromDate() {
        return fromDate;
    }

    public void setFromDate(Date fromDate) {
        this.fromDate = fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public void setToDate(Date toDate) {
        this.toDate = toDate;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getPriceMember() {
        return priceMember;
    }

    public void setPriceMember(Integer priceMember) {
        this.priceMember = priceMember;
    }

    @XmlTransient
    public Collection<ActionPrice> getActionPriceCollection() {
        return actionPriceCollection;
    }

    public void setActionPriceCollection(Collection<ActionPrice> actionPriceCollection) {
        this.actionPriceCollection = actionPriceCollection;
    }

    public FyxAddress getIdAddress() {
        return idAddress;
    }

    public void setIdAddress(FyxAddress idAddress) {
        this.idAddress = idAddress;
    }
    
    public float getActualPrice(){
        long now = new Date().getTime();
        for(ActionPrice actionPrice : actionPriceCollection){
            if(actionPrice.getFromDate().getTime() < now && 
                    actionPrice.getToDate().getTime() > now){
                return actionPrice.getPrice();
            }
        }
        return price;
    }
    
    public float getActualPriceMember(){
        long now = new Date().getTime();
        for(ActionPrice actionPrice : actionPriceCollection){
            if(actionPrice.getFromDate().getTime() < now && 
                    actionPrice.getToDate().getTime() > now){
                return actionPrice.getPriceMember();
            }
        }
        return price;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idAction != null ? idAction.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Action)) {
            return false;
        }
        Action other = (Action) object;
        if ((this.idAction == null && other.idAction != null) || (this.idAction != null && !this.idAction.equals(other.idAction))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "cz.makrobioklub.action.entity.Action[ idAction=" + idAction + " ]";
    }
    
}
